package com.livvy.exer.test;

/**
 * Copyright livvy guo and other contributors
 * Released under the MIT license
 * <p/>
 * Created with IntelliJ IDEA.
 * User: GuoZheng
 * Date: 13-11-18
 * Time: 下午1:46
 */
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class HtmlMail
{

    public HtmlMail()
    {
        try	{
            String from = "livvyguo@163.com";
            String to = "975605396@qq.com";
            String body = "<HTML><font color=blue>This is </font><font color=blue>HTML</font></HTML>";
            String subject = "HTML";


            MimeMessage message = new MimeMessage(getLocalSession());
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(subject);


            Multipart multipart = new MimeMultipart();

            BodyPart htmlPart = new MimeBodyPart();

            htmlPart.setContent(body,"text/html");
            htmlPart.setDisposition(BodyPart.INLINE);
            multipart.addBodyPart(htmlPart);



            message.setContent(multipart);


            Transport.send(message);

        }	catch (Exception e)	{
            e.printStackTrace();
        }

    }

    public static Session getLocalSession()	{
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "123.125.50.135");
        props.put("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");

        return Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("user@163.com","user password");
            }
        });
    }


    public static void main(String[] args) {
        new HtmlMail();
    }


}
